home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 706 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.4 KB

  1. Path: inforamp.net!usenet
  2. From: pcurran@inforamp.net (Peter Curran)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Restrictions on qsort compare function?
  5. Date: Sat, 06 Apr 1996 17:23:42 GMT
  6. Organization: PSC Enterprises
  7. Message-ID: <4k69bf$ehg@sam.inforamp.net>
  8. References: <4iokop$h4p@lyra.csx.cam.ac.uk> <4iqjar$2m9@usenet.pa.dec.com> <4jgltp$f9l@lyra.csx.cam.ac.uk> <828644274snz@genesis.demon.co.uk> <4k28t4$2g0@sam.inforamp.net> <828726582snz@genesis.demon.co.uk>
  9. Reply-To: pcurran@inforamp.net
  10. NNTP-Posting-Host: ts15-06.tor.istar.ca
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. On Fri, 05 Apr 96 17:49:42 GMT in article <828726582snz@genesis.demon.co.uk>
  14.     Lawrence Kirby <fred@genesis.demon.co.uk> (Lawrence Kirby) wrote:
  15.  
  16. >If you can find any definition as to what the behaviour should be with your
  17. >comparison function, explain what it is. Otherwise the behaviour is
  18. >undefined.
  19.  
  20. IMHO, qsort() is required to return an array that is sorted according to the
  21. criteria implied by the comparison function.  That is, at the point where qsort
  22. returns, the array must match the order implied by the comparison function.  If
  23. the comparison function definition is such that the order changes (although the
  24. *definition* of the order must remain fixed), then IMHO the current wording of
  25. the standard can be read as meaning that it is qsort's problem to deal with it.
  26. This all hinges on how one interprets the word "considered" in the standard -
  27. that is a very vague term to use in a standard, and IMHO can reasonably lead to
  28. confusion.  If a "snapshot" is taken at the time qsort() returns, the array must
  29. match order implied by the comparison function.  If the definition of the
  30. comparison criteria cannot provide a definite sort order for the array at any
  31. instant, then I agree that it is invalid, but if it does provide such an
  32. definite order at any instant, then I think the current standard permits it.
  33.  
  34. Let me give another example of a problematic comparison function that seems to
  35. me to satisfy all the requirements of the standard, but leads, I think, to
  36. unintended problems for implementing qsort.
  37.  
  38. The comparison function returns 1 if the first value is greater than the second
  39. value, or -1 if the second   However, if they are equal, the function then
  40. compares the values of the pointers to the parameters (the actual arguments of
  41. the function) - if the first is greater, 1 is returned, if the second is greater
  42. -1 is returned, otherwise 0 is returned.
  43.  
  44. This function is a (misguided) attempt to create a stable sort.  It is only a
  45. valid comparison function if qsort() only passes pointers to elements in the
  46. array being sorted to the comparison function - otherwise, the pointer
  47. comparisons are (probably) undefined.  This comparions function will "work"
  48. (i.e. not produce undefined behaviour) for some possible implementations of
  49. qsort(), but not others.  It would also produce a consistent sort order for some
  50. implementations, but not others.  (I think Bubble Sort could be implemented with
  51. these restriction.  Quicksort would be trickier, or a lot slower.)
  52.  
  53. So - is this comparison function legal or not?  I cannot see anything in the
  54. standard that disallows it.  I think the requirement to avoid undefined
  55. behaviour in this case can reasonably be interpreted as falling on qsort(), not
  56. on the comparison function.  I am sure the committee never intended such a
  57. think, but I the current standard can reasonably be read as containing them.
  58.  
  59. --
  60. Peter Curran                               pcurran@inforamp.net
  61.  
  62.